package sel.me.spring.boot159.web.extension.postprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

/**
 * spring中的BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口，BeanFactoryPostProcessor的作用是在bean
 *
 * 的定义信息已经加载但还没有初始化的时候执行方法postProcessBeanFactory(）方法，而BeanDefinitionRegistryPostProcessor是在
 *
 * BeanFactoryPostProcessor的前面执行。
 * @author shenenlu 2021年05月18日 下午17:56:27
 */
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {

//        RootBeanDefinition beanDefinition = new RootBeanDefinition(MyServiceImpl.class); //Service实现
//        serviceDefinition.setTargetType(MyService.class); //Service接口
//        serviceDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
//        beanDefinitionRegistry.registerBeanDefinition("myBeanName", beanDefinition );


//                for (int i = 0; i < 10; i++) {
//            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
//            builder.addPropertyValue("name", "sfz_" + i);
//            beanDefinitionRegistry.registerBeanDefinition("person" + i, builder.getBeanDefinition());
//        }

    }

    @Override
    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        int beanDefinitionCount = configurableListableBeanFactory.getBeanDefinitionCount();
        System.out.println("MyBeanDefinitionRegistryPostProcessor...postProcessBeanFactory...的bean数="+beanDefinitionCount);
    }
}
